package com.java.mywxmp.handler;

import com.java.mywxmp.constant.UserConstant;
import com.java.mywxmp.entity.User;
import com.java.mywxmp.service.UserService;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * 用户取消关注关注事件
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UnsubscribeHandler extends AbstractHandler {
    private final UserService userService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        String openId = wxMessage.getFromUser();
        this.logger.info("取消关注用户 OPENID: " + openId);
        // 更新本地数据库为取消关注状态
        User user = new User();
        user.setOpenid(wxMessage.getFromUser());
        user.setStatus(UserConstant.UN_FOLLOW);
        userService.save(user);
        return null;
    }

}
